package ebnf.ebnfGrammar;

import java.util.ArrayList;

import ebnf.ebnfScannerParser.EBNFTokenKinds;
import ebnf.ebnfScannerParser.EBNFToken;

/**
 * 
 * @author Sander Meijer & Bart Wentink
 */
public class Expression implements EBNFTokenKinds {
	private ArrayList<EBNFToken> tokenList;
	private int containsEmpty = -1; // -1 if there are no tokens yet,
									// 0 if EMPTY tokens are not allowed
									// 1 if there is an EMPTY token
	private Boolean empty = false;

	public Expression() {
		tokenList = new ArrayList<EBNFToken>();
	}

	/**
	 * Adds all {@link EBNFToken} that are not of {@link EBNFTokenKinds} EMPTY. <br>
	 * If the token is EMPTY it will only be added if allowed.
	 * 
	 * @param {@link EBNFToken} token
	 * @return {@link Boolean} success
	 */
	public boolean addToken(EBNFToken token) {
		if (containsEmpty == -1 && token.getKind() == EMPTY) {

			if (token != null) {
				containsEmpty = 1;
				empty = true;
				tokenList = null;
				return true;
			}
		} else if (containsEmpty != 1 && token.getKind() != EMPTY) {
			if (containsEmpty == -1)
				containsEmpty = 0;
			tokenList.add(token);
			return true;
		}
		return false;
	}

	/**
	 * Returns a boolean to see if there is an empty.
	 * 
	 * @return {@link Boolean} hasEmpty
	 */
	public boolean isEmpty() {
		return empty;
	}
	
	public ArrayList<EBNFToken> getTokens(){
		return tokenList;
	}

	public EBNFToken getFirst(){
		return tokenList.get(0);
	}
	
	/**
	 * Generates a string
	 * 
	 * @return {@link String} string
	 */
	public String toString() {
		String s = "";
		if (containsEmpty < 1) {
			for (EBNFToken t : tokenList) {
				s += t.getText() + " ";
			}
		} else {
			s += "@ ";
		}
		return s;
	}
}
